<%@ page import="java.util.LinkedList"%>
<%@page import="java.io.*" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ page import="javax.xml.parsers.DocumentBuilderFactory,javax.xml.parsers.DocumentBuilder,
					org.w3c.dom.*"%>
<%@page import ="org.jfree.data.category.*"%>
<%@page import="org.jfree.chart.*" %>
<%@page import="org.jfree.chart.servlet.ServletUtilities" %>
<%@page import="org.jfree.chart.entity.*" %>
<%@page import ="org.jfree.chart.JFreeChart"%>
<%@page import ="org.jfree.chart.plot.SpiderWebPlot" %>
<%@page import ="org.jfree.chart.title.*" %>
<%@page import ="org.jfree.ui.RectangleEdge" %>
<%@page import ="ctm.javacode.MySpiderWebPlot" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>問卷</title>
<style type="text/css">
<!--
body {
	background-image: url(img/grass.jpg);
    background-position: center top;
	background-color: #FFFFFF;
	background-repeat: repeat;
}
body,td,th {
	color: #000000;
	font-size:24px;
	font-family: Arial;
}


a:link {
text-decoration:none;
font-family:Arial;
font-size:20px;
color:#FFFFFF;
}
a:visited {
text-decoration:none;
font-family:Arial;
font-size:20px;
color:#FFFFFF;
}
a:hover {
text-decoration:none;
font-family:Arial;
font-size:20px;
color:#000000;
}


.ind:link {
color: #535353;
text-decoration:none;
font-family:"標楷體";
font-size:18px;
}
.ind:visited {
color:#535353;
text-decoration:none;
font-family:"標楷體";
font-size:18px;
}

.ind:hover {
color:#BB280F;
text-decoration:none;
font-family:"標楷體";
font-size:18px;
}


.HEAD_STYLE {
	font-weight:bold;
	color: #1B4001;
	font-family:"標楷體";
	font-size:30px; 
}
.TEXT_STYLE {
	font-family:Arial;
	color:#193940;
	font-size:24px;
	font-weight:bold;
}
.BOTTOM_STYLE {
	color: #FFFFFF; 
	font-family: Arial; 
	font-size:20px;
}
.LEFT_BUTTON {
	font-family:"標楷體"; 
	font-size:30px;
}

-->
</style>

</head>
<body alink="#FF0000" vlink="#FF0000">
<table width="100%" border="0">
		<tr>
			<td><p>&nbsp;</p></td>
		</tr>
				
			
			
		
 
    		<td width="5%">&nbsp;</td>
			
			<td bgcolor="#FFFFFF"><table width="100%" border="0">
      			<tr>
        			<td background="img/herbal.jpg">
						<p>&nbsp;</p>
          				<p>&nbsp;</p>
          				<p>&nbsp;</p>
          				<p>&nbsp;</p>
		  				<p>&nbsp;</p>
          
          			</td>
			  </tr>
     
	 
	 
	 
	  			  <tr>
        			<td>	
						<table bgcolor="#437A27" width="100%" border="0">
         					 <tr>
            					<td>&nbsp;</td>
								<td width="20%" align="center"><a href="main.jsp" target="_parent">回到主選單</a></td>
           			 			
          			 		</tr>
        				</table>
					</td>
     			  </tr>
      
	  			
	  
	  
	  
	 			  <tr>
        				<td>
							<table width="100%">
           						<tr>
									<td valign="top">
										<table width="100%">
											
								
											<tr>
												
											<td>
											<%
	//String loginName = (request.getSession().getAttribute("login")).toString();
String loginName = (String)(request.getSession().getAttribute("login"));
   	String filename = request.getParameter("filename");
	String category = request.getParameter("category");
%>
<% //讀user所填的歷史問卷資料(xml)
	Document history = null;
	File file = new File("C:/history/"+loginName+"/"+filename+".xml");
	DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
	try{
		DocumentBuilder builder = factory.newDocumentBuilder();
		history = builder.parse(file);
		
	}catch(Exception e){
		out.print("XML analysis error" + e.getMessage());
	}
	
	int quesflag=0,tranValueflag=0,typePrintflag=0;
	LinkedList<String> QuesFromtype = new LinkedList<String>(); //放每個類型的題目
	LinkedList<String> AnsFromtype = new LinkedList<String>(); //放user填的每個類型題目之答案
	LinkedList<String> ClassForValue = new LinkedList<String>(); //型別(中文)
	LinkedList<Double> transValue = new LinkedList<Double>();  //放user填完答案問卷之分數結果
	LinkedList<String> typeForPrint = new LinkedList<String>(); //放user體值判斷的結果
	
	Element elementforUser = history.getDocumentElement();
	NodeList list1 = elementforUser.getChildNodes();
	for(int i=0 ; i<list1.getLength();i++){
		 Node temp1 = list1.item(i);
		 if(isTextNode(temp1)){
			 continue;
		 }
		 if(temp1.getNodeName() == "question"){
			 quesflag = 1;
		 }
		 if(temp1.getNodeName() == "transValue"){
			 tranValueflag = 1;
		 }
		 if(temp1.getNodeName() == "typePrint"){
			 typePrintflag = 1;
		 }		 
		 NodeList nodes2 = temp1.getChildNodes();
		 for(int j=0;j < nodes2.getLength();j++){
			 Node temp2 = nodes2.item(j);
			 if(isTextNode(temp1)){
				 continue;
			 }
			 if(quesflag == 1 && temp2.hasChildNodes()){
				 QuesFromtype.add(temp2.getNodeName());
				 AnsFromtype.add(temp2.getFirstChild().getNodeValue());
			 }
			 if(tranValueflag == 1 && temp2.hasChildNodes()){
				 ClassForValue.add(temp2.getNodeName());
				 transValue.add(Double.parseDouble(temp2.getFirstChild().getNodeValue()));
			 }
			 if(typePrintflag == 1 && temp2.hasChildNodes()){
				 typeForPrint.add(temp2.getFirstChild().getNodeValue());
			 }
			 
		 }
		 quesflag = 0;
		 tranValueflag = 0;
		 typePrintflag = 0;
	}
%>	
<%
	//讀對應的問卷題目(xml)
	String path;
	if(category.equals("A")){
		path = "./WEB-INF/tcm.xml";
	}
	else{
		path = "./WEB-INF/tcm2.xml";
	}
	Document BookDom = null;
	String File2 = request.getRealPath(path);
	DocumentBuilderFactory factory2 = DocumentBuilderFactory.newInstance();
	
	try{
		DocumentBuilder builder = factory2.newDocumentBuilder();
		BookDom = builder.parse(File2);
		
	}catch(Exception e){
		out.print("XML analysis error" + e.getMessage());
	}
	
	int questionflag=0,marklistflag=0;
	LinkedList<String> questions = new LinkedList<String>(); //放題目
	LinkedList<String> marklists = new LinkedList<String>(); //放等級
	LinkedList<String> CLASS = new LinkedList<String>();//放型別
	LinkedList<String> CLASSforQ = new LinkedList<String>();//放根據型別的題目
	
	Element element = BookDom.getDocumentElement();
	NodeList nodes1 = element.getChildNodes();
	for(int i=0;i < nodes1.getLength() ; i++){
		Node temp1 = nodes1.item(i);
		NamedNodeMap attrlist1 = temp1.getAttributes();
		if(attrlist1 != null)
		{
			//System.out.println(attrlist1.item(0).getNodeName());
			//System.out.println(attrlist1.item(0).getNodeValue());
		}
		if(isTextNode(temp1)){
			continue;
		}
		if(temp1.getNodeName() == "QUESTION"){
			questionflag=1;
		}
		if(temp1.getNodeName() == "MARKLIST"){
			marklistflag=1;
		}
		NodeList nodes2 = temp1.getChildNodes();
		for(int j=0;j < nodes2.getLength();j++){
			Node temp2 = nodes2.item(j);
			NamedNodeMap attrlist2 = temp2.getAttributes();
			if(attrlist2 != null && temp2.getNodeName() == "CLASS"){
				CLASS.add(attrlist2.item(0).getNodeValue());
				//System.out.println(attrlist2.item(0).getNodeName());
				//System.out.println(attrlist2.item(0).getNodeValue());
			}

			if(isTextNode(temp2)){
				continue;
			}
			if(questionflag == 1){
				questions.add(temp2.getFirstChild().getNodeValue());
			}
			if(marklistflag == 1){
				marklists.add(temp2.getFirstChild().getNodeValue());
				//System.out.println(marklists.peekLast());
			}
			if(temp2.hasChildNodes() && temp2.getNodeName() == "CLASS"){
				NodeList nodes3 = temp2.getChildNodes();
				for(int k=0; k<nodes3.getLength();k++){
					Node temp3 = nodes3.item(k);
					if(isTextNode(temp3)){
						continue;
					}
					CLASSforQ.add(temp3.getNodeName());
					//System.out.println(":"+CLASSforQ.peekLast());
				}
				CLASSforQ.add("end");
				//System.out.println(":"+CLASSforQ.peekLast());
			}

		}
		questionflag = 0;
		marklistflag = 0;
	}
	//將讀進來的體質由中文轉變成英文(供radio按鈕命名用)
	String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	LinkedList<String> CLASStoEng = new LinkedList<String>();//放型別(英文型式)
	for(int i=0 ; i < CLASS.size();i++){
		CLASStoEng.add(alphabet.substring(i, i+1));
	}
%>	
<%!
	public boolean isTextNode(Node node){
		return node.getNodeName().equals("#text");
	}
%>
<%
	int j=0,count=0,levels=0,id=0,count1=0;
	for(int i=0;i<CLASS.size();i++){
%>
		<center><font size="5" style="font: bold normal bold 18pt/30pt '標楷體'" color="66CC33"><%= CLASS.get(i) %></font></center>
		<br>
		<table align="center" border="2" width="700">
				<tr bgcolor="FFFF99">
					<td style="color:#193940;">請根據近一年的體驗和感覺,回答下列問題</td>
<%
		while(levels < marklists.size()){
%>
					<td align="center"><%= marklists.get(levels) %></td>
<%
			levels++;
		}
%>
				</tr>
<% 				
		while(j < CLASSforQ.size()){
			if(CLASSforQ.get(j).equals("end")){				
				break;
			}
			String ques = CLASSforQ.get(j).substring(1);
%>
			<tr><td width="450"><%= "("+(count+1)+")" %><%= questions.get((Integer.parseInt(ques))-1) %></td>
				<td><center><label for="type<%=CLASStoEng.get(i)%><%= ques%>"><% if(check(CLASStoEng.get(i),ques,QuesFromtype,AnsFromtype,1))out.print("●"); %></label></center></td>
				<td><center><label for="type<%=CLASStoEng.get(i)%><%= ques%>"><% if(check(CLASStoEng.get(i),ques,QuesFromtype,AnsFromtype,2))out.print("●"); %></label></center></td>
				<td><center><label for="type<%=CLASStoEng.get(i)%><%= ques%>"><% if(check(CLASStoEng.get(i),ques,QuesFromtype,AnsFromtype,3))out.print("●"); %></label></center></td>
				<td><center><label for="type<%=CLASStoEng.get(i)%><%= ques%>"><% if(check(CLASStoEng.get(i),ques,QuesFromtype,AnsFromtype,4))out.print("●"); %></label></center></td>
				<td><center><label for="type<%=CLASStoEng.get(i)%><%= ques%>"><% if(check(CLASStoEng.get(i),ques,QuesFromtype,AnsFromtype,5))out.print("●"); %></label></center><% count++; %></td>
			</tr>
<%
			j++;
		}
%>
		</table>
		<br>
<%
		j++;
		count=0;
		levels=0;
	}
%>
<%!
	public boolean check(String Class,String ques,LinkedList<String> QuesFromtype,LinkedList<String> AnsFromtype,int value){
		boolean flag = false;
		String type;
		for(int i=0;i<QuesFromtype.size();i++){
			type = "type"+Class+ques;
			if(QuesFromtype.get(i).equals(type)){
				if(Integer.parseInt(AnsFromtype.get(i)) == value){
					flag = true;
				}
				break;
			}
			else;
		}
		return flag;
	}
%>
<center><font color="66CC33" size="5" class="HEAD_STYLE" style="font: bold normal bold 20pt/32pt '標楷體'">體質自測結果:</font><br>
<br></center>
<%	//印出屬於哪種體質
	for(int i=0;i<typeForPrint.size();i++){
%>
		<center><font style="font: bold normal bold 14pt/26pt '標楷體'"><%= typeForPrint.get(i) %></font></center><br>		
<%	
	}
%>
<%	//Show出雷達圖
	DefaultCategoryDataset dataset = new DefaultCategoryDataset();
	String group1="";
	
	int index=0;
	while(index < transValue.size() && index < ClassForValue.size()){
		dataset.addValue(transValue.get(index), group1 ,ClassForValue.get(index));
		index++;
	}
	MySpiderWebPlot spiderwebplot = new MySpiderWebPlot(dataset);
	JFreeChart jfreechart = new JFreeChart("", TextTitle.DEFAULT_FONT,spiderwebplot, false);
	final ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
	LegendTitle legendtitle = new LegendTitle(spiderwebplot);
	legendtitle.setPosition(RectangleEdge.BOTTOM);
	jfreechart.addSubtitle(legendtitle);
	          String fname = ServletUtilities.saveChartAsJPEG(jfreechart, 600, 400, info, session);
	PrintWriter pw = new PrintWriter(out);
	ChartUtilities.writeImageMap(pw, fname, info, false);
	pw.flush();
	String chartURL = request.getContextPath() + "/servlet/DisplayChart?filename=" + fname;
%>
<center><font size="5" style="font: bold normal bold 16pt/28pt '標楷體'" color="3333FF">體質自測雷達圖</font><br></center>
	<center><IMG SRC="<%= chartURL %>" WIDTH="600" HEIGHT="400"
	BORDER="0" USEMAP="#<%= fname %>"></center>
											
											</td>
											</tr>
											
										</table>
									
             

		
											
								  </td>
									

								</tr>
       					 </table>
         

         				</td>
			  </tr>
   
	  
					 <tr>
      					  
   					    <td><p>&nbsp;</p></td>
  					 </tr>
	  				
					<tr>
      					  <td bgcolor="#437A27"><div align="center" class="BOTTOM_STYLE">Copyright@CCU_PLlab</div></td>
   
			  </tr>
    						
  	 			  
	
			</table>
		</td>
	    
		<td width="5%">&nbsp;</td>
</table>
<p>&nbsp;</p>
</body>
</html>
